package com.my.java8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
/**
 * Hello world!
 *
 */
public class App {
	
	
	
	public interface Predicate<T>{
		boolean test(T t);
	}
	
	public static <T> List<T> filter(List<T> list,Predicate<T> p){
		List<T> result = new ArrayList<>();
		for(T e:list){
			if(p.test(e)){
				result.add(e);
			}
		}
		
		return result;
	}
	
	
    public static void main( String[] args ){
        System.out.println( "Hello World!" );
        
        List<Apple> list = new ArrayList<Apple>(){{
        	add(new Apple("red"));
        	add(new Apple("black"));
        	add(new Apple("white"));
        	add(new Apple("yellow"));
        	add(new Apple("green"));
        }};
        
        System.out.println(filter(list, (Apple a)->a.getColor().equals("red")));
        
        list.sort((Apple a,Apple b)->a.getColor().compareTo(b.getColor()));
        System.out.println(list);
        
        Thread t = new Thread(()->System.out.println("new thread"));
        t.start();
        
        
        Comparator<String> cop = Comparator.comparingInt(String::length);
        Comparator<String> cop2 = cop.thenComparing(s->s.chars().count());
        
        Arrays.asList("3333","333","sdfs").stream().sorted(cop2).collect(Collectors.toList());
        
        List<Integer> numbers = Arrays.asList(2, 3, 4, 5);
        
        
        numbers.stream().peek(x->System.out.println("from stream: "+x))
        	.map(x->x+3).peek(x->System.out.println("from stream: "+x)).collect(Collectors.toList());
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    }
}
